package com.lg.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lg.product.entity.Category;
import com.lg.product.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 商品类目表，类目和商品(spu)是一对多关系，类目与品牌是多对多关系 前端控制器
 * </p>
 *
 * @author song
 * @since 2020-07-22
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private ICategoryService iCategoryService;

    /**
     * 分类 查询列表
     * @param parentId
     * @return
     */
    @RequestMapping("list")
    public List<Category> list(@RequestParam(value = "parentId",defaultValue = "0") Long parentId){
        return iCategoryService.listByparentId(parentId);
    }

    /**
     * 分类 添加
     * @param category
     * @return
     */
    @RequestMapping("addCategory")
    public int addCategory(@RequestBody Category category) {
        int i = iCategoryService.addCategory(category);
        return i;
    }

    /**
     * 分类 回显
     * @param id
     * @return
     */
    @RequestMapping("listId")
    public Category listId(Integer id){
        Category category = iCategoryService.listId(id);
        return category;
    }

    /**
     * 分类 修改
     * @param category
     * @return
     */
    @RequestMapping("updateCategory")
    public int updateCategory(@RequestBody  Category category){
        return iCategoryService.updateCategory(category);
    }

    /**
     * 分类 删除
     * @param id
     * @return
     */
    @RequestMapping("deleteCategory")
    public int deleteCategory(Integer id){
        return iCategoryService.deleteCategory(id);
    }

    /**
     * 分类 根据 parentId 查询
     * @param parentId
     * @return
     */
    @RequestMapping("parentIdList")
    public List<Category> parentIdList( Long parentId){
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.eq("parent_id",parentId);
        List<Category> list = iCategoryService.list(categoryQueryWrapper);
        return list;
    };

    @RequestMapping("categoryList")
    public List<Category> categoryList(){
        Integer parentId=0;
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.eq("parent_id",parentId.longValue());
        List<Category> list = iCategoryService.list(categoryQueryWrapper);
        list.forEach(res->{
            QueryWrapper<Category> categoryQueryWrapper1 = new QueryWrapper<>();
            categoryQueryWrapper1.eq("parent_id",res.getId());
            List<Category> list2 = iCategoryService.list(categoryQueryWrapper1);
            res.setCategoryList(list2);
        });

        return list;
    }
}
